package com.coolw.spring.statemachine.support.order;

import com.coolw.spring.statemachine.constant.StateMachineConst;
import com.coolw.spring.statemachine.enums.OrderEvent;
import com.coolw.spring.statemachine.enums.OrderStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.guard.Guard;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 订单状态转换保护,只有满足条件才允许发生状态转换
 *
 * @author coolw
 * @date 2024/4/15 10:23
 */
@Slf4j
@Component
public class OrderStateMachineGuard implements Guard<OrderStatusEnum, OrderEvent> {

    @Override
    public boolean evaluate(StateContext<OrderStatusEnum, OrderEvent> context) {
        OrderContext orderContext = (OrderContext) context.getMessageHeader(StateMachineConst.CONTEXT_KEY);
        if (Objects.isNull(orderContext)) {
            log.warn("orderContext为空,context:{}", context);
            return false;
        }
        return true;
    }
}
